//
//  NSMutableArray+Safe.m
//  niuguwang
//
//  Created by BrightLi on 2021/5/15.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

#import "NSMutableArray+Safe.h"
#import "YMSafeUtil.h"

@implementation NSMutableArray(Safe)

#if __has_feature(objc_arc)
#error NSMutableArray+Safe必须是MRC.请使用编译参数: -fno-objc-arc
#endif

+ (void) safe_fixed
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cls=NSClassFromString(@"__NSArrayM");
        [YMSafeUtil swizzleInstance:cls
                    sel1:@selector(objectAtIndexedSubscript:)
                    sel2:@selector(safe_objectAtIndexedSubscript:)];
        [YMSafeUtil swizzleInstance:cls
                    sel1:@selector(addObject:)
                    sel2:@selector(safe_addObject:)];
        [YMSafeUtil swizzleInstance:cls
                    sel1:@selector(objectAtIndex:)
                    sel2:@selector(safe_objectAtIndex:)];
        [YMSafeUtil swizzleInstance:cls
                    sel1:@selector(setObject:atIndexedSubscript:)
                    sel2:@selector(safe_setObject:atIndexedSubscript:)];
        [YMSafeUtil swizzleInstance:cls
                    sel1:@selector(removeObjectAtIndex:)
                    sel2:@selector(safe_removeObjectAtIndex:)];
        [YMSafeUtil swizzleInstance:cls
                    sel1:@selector(insertObject:atIndex:)
                    sel2:@selector(safe_insertObject:atIndex:)];
        [YMSafeUtil swizzleInstance:cls
                    sel1:@selector(replaceObjectAtIndex:withObject:)
                    sel2:@selector(safe_replaceObjectAtIndex:withObject:)];
        [YMSafeUtil swizzleInstance:cls
                    sel1:@selector(getObjects:range:)
                    sel2:@selector(safe_getObjects:range:)];
    });
}

- (id) safe_objectAtIndexedSubscript:(NSUInteger) index
{
    if(index>=self.count){
        NSString *info=[NSString stringWithFormat:@"! [__NSArrayM objectAtIndexedSubscript:]索引[%tu]越界,数组长度:[%tu]",index,self.count];
        [YMSafeUtil log:@"%@", info];
        return nil;
    }
    
    return  [self safe_objectAtIndexedSubscript:index];
}

- (void) safe_addObject:(id)anObject
{
    if(!anObject)
    {
        NSString *info=[NSString stringWithFormat:@"! [__NSArrayM addObject:]不能加入空值",nil];
        [YMSafeUtil log:@"%@", info];
        return;
    }
    [self safe_addObject:anObject];
}
// 访问下标
- (id) safe_objectAtIndex:(NSUInteger)index
{
    if (index <self.count){
        return [self safe_objectAtIndex:index];
    }
    NSString *info=[NSString stringWithFormat:@"* [__NSArrayM objectAtIndex:]索引[%ld]出界[%ld]",(long)index,(long)self.count];
    [YMSafeUtil log:@"%@", info];
    return nil;
}

- (void) safe_setObject:(id) obj atIndexedSubscript:(NSUInteger)index
{
    if(!obj){
        NSString *info=[NSString stringWithFormat:@"* [__NSArrayM setObject:atIndexedSubscript:]:不能设置空值",nil];
        [YMSafeUtil log:@"%@", info];
        return;
    }
    if(index>self.count){
        NSString *info=[NSString stringWithFormat:@"! [__NSArrayM setObject:atIndexedSubscript:]索引[%lu]出界,数组长度:[%lu]",
                        (unsigned long)index,(unsigned long)self.count];
        [YMSafeUtil log:@"%@", info];
        return;
    }
    [self safe_setObject:obj atIndexedSubscript:index];
}

- (void) safe_insertObject:(id)anObject atIndex:(NSUInteger)index
{
    if (!anObject) {
        NSString *info=[NSString stringWithFormat:@"! [__NSArrayM safe_insertObject:atIndex:]插入不能为空值"];
        [YMSafeUtil log:@"%@", info];
        return;
    }
    if(index>self.count){
        NSString *info=[NSString stringWithFormat:@"! [__NSArrayM safe_insertObject:atIndex:]索引[%lu]出界[%@]",
                        (unsigned long)index,anObject];
        [YMSafeUtil log:@"%@", info];
        return;
    }
    [self safe_insertObject:anObject atIndex:index];
}

- (void) safe_removeObjectAtIndex:(NSUInteger)index
{
    if(index>=self.count){
        NSString *info=[NSString stringWithFormat:@"! [__NSArrayM removeObjectAtIndex:]索引[%lu]出界,数组长度:[%lu]",
                        (unsigned long)index,(unsigned long)self.count];
        [YMSafeUtil log:@"%@", info];
        return;
    }
    [self safe_removeObjectAtIndex:index];
}
// 替换元素检查
- (void) safe_replaceObjectAtIndex:(NSUInteger)index
                     withObject:(id)anObject
{
    if (!anObject) {
        NSString *info=@"[__NSArrayM safe_replaceObjectAtIndex:withObject:]替换不能为空值";
        [YMSafeUtil log:@"%@", info];
        return;
    }
    if (index >= self.count) {
        NSString *info=[NSString stringWithFormat:@"[__NSArrayM safe_replaceObjectAtIndex:withObject:]索引[%tu]出界[%@",
        index,anObject];
        [YMSafeUtil log:@"%@", info];
        return;
    }
    [self safe_replaceObjectAtIndex:index withObject:anObject];
}

- (void) safe_getObjects:(__unsafe_unretained id _Nonnull *)objects
                range:(NSRange)range
{
    @try {
        [self safe_getObjects:objects range:range];
    } @catch (NSException *exception) {
        [YMSafeUtil stackLog:exception start:3];
    } @finally {
    }
}

@end

